home *** CD-ROM | disk | FTP | other *** search
- /*
- * John DiMarco University of Toronto, CSRI
- */
- #include <stdio.h>
- #include <strings.h>
- #include <varargs.h>
- #include "utils.h"
-
- #ifdef lint
- #undef va_arg(x,y)
- #define va_arg(x,y) (y)NULL
- #endif
-
- extern char *progname;
- extern int d;
-
- extern char *malloc(), *realloc();
-
- /*
- * Error(): behaves like fprintf(stderr, ...) followed by exit(2), except
- * that the 'programname: ' preceeds the print, and a newline
- * follows it.
- */
- /*VARARGS*/
- void Error(va_alist)
- va_dcl
- {
- va_list args;
- char *format;
-
- va_start(args);
- format = va_arg(args, char *);
- fprintf(stderr, "%s: ", progname);
- vfprintf(stderr, format, args);
- fprintf(stderr, "\n");
- va_end(args);
- (void)exit(2);
- }
-
- /*
- * dfprintf(): behaves like fprintf, except the first argument must be a
- * debugging level. The message will only be printed if "d"
- * is equal to or greater than the debugging level.
- */
- /*VARARGS*/
- void dfprintf(va_alist)
- va_dcl
- {
- va_list args;
- int debugLevel;
- FILE *stream;
- char *format;
-
- va_start(args);
- debugLevel = va_arg(args, int);
- stream = va_arg(args, FILE *);
- format = va_arg(args, char *);
- if(d >= debugLevel){
- vfprintf(stream, format, args);
- }
- va_end(args);
- }
-
- /*
- * Warning(): behaves like Error, except returns rather than exits.
- */
- /*VARARGS*/
- void Warning(va_alist)
- va_dcl
- {
- va_list args;
- char *format;
-
- va_start(args);
- format = va_arg(args, char *);
- fprintf(stderr, "%s: ", progname);
- vfprintf(stderr, format, args);
- fprintf(stderr, "\n");
- va_end(args);
- }
-
- FILE *efopen(file, mode)
- char *file, *mode;
- {
- FILE *fp;
- if (NULL!=(fp=fopen(file,mode)))
- return(fp);
- Error("can't open file \"%s\" mode \"%s\"", file, mode);
- /*NOTREACHED*/
- }
-
- void efclose(f)
- FILE *f;
- {
- if(EOF==fclose(f))
- Error("can't close file");
- /*NOTREACHED*/
- }
-
- /*
- * mylib_malloc(): Checks if it gets a NULL pointer, calls Error if so.
- */
- char *mylib_malloc(size, file, line)
- unsigned size;
- char *file;
- int line;
- {
- char *result;
-
- result = malloc(size);
- if(NULL==result){
- Error("Out of memory at line %d in \"%s\".", line, file);
- }
- return(result);
- }
-
- /*
- * mylib_realloc(): Checks if it gets a NULL pointer, calls Error if so.
- */
- char *mylib_realloc(ptr, size, file, line)
- char *ptr, *file;
- unsigned size;
- int line;
- {
- char *result;
-
- result = realloc(ptr, size);
- if(NULL==result){
- Error("Out of memory at line %d in \"%s\".", line, file);
- }
- return(result);
- }
-
- /*
- * mylib_scopy(): Takes a string and creates a new physical copy of it.
- */
- char *mylib_scopy(string, file, line)
- char *string, *file;
- int line;
- {
- char *result;
-
- result = malloc((unsigned)strlen(string)+1);
-
- if(NULL==result){
- Error("Out of memory at line %d in \"%s\".", line, file);
- }
- (void)strcpy(result, string);
- return(result);
- }
-
- /*
- * mylib_srcopy(): Reallocs first string to make room for second, copies it.
- */
- char *mylib_srcopy(s1, s2, file, line)
- char *s1, *s2, *file;
- int line;
- {
- s1=mylib_realloc(s1, (unsigned)strlen(s2)+1, file, line);
- (void)strcpy(s1, s2);
- return(s1);
- }
-
- /*
- * cat(): Take a list of strings, followed by NULL, return their concatenation
- * in malloc'ed space.
- */
- /*VARARGS*/
- char *cat(va_alist)
- va_dcl
- {
- va_list args;
- unsigned length=1;
- char *str, *newstr;
-
- /* get length */
- va_start(args);
- while(1){
- str = va_arg(args, char *);
- if(NULL!=str){
- length+=strlen(str);
- } else {
- break;
- }
- }
- va_end(args);
-
- newstr=malloc(length);
- if(NULL==newstr) Error("Out of memory in cat()");
-
- newstr[0]=(char)0;
-
- /* create string */
- va_start(args);
- while(1){
- str = va_arg(args, char *);
- if(NULL!=str) {
- (void)strcat(newstr, str);
- } else {
- break;
- }
- }
- va_end(args);
- #ifdef lint
- args=args; /* make lint shut up about "args set but not used" */
- #endif
- return(newstr);
- }
-
- /*
- * getstr(): read a string of arbitrary length from the given file
- * descriptor into malloc'ed memory, up to (but not including)
- * the next newline. Return a pointer to the string; NULL if EOF.
- */
- char *getstr(fd)
- FILE *fd;
- {
- unsigned int buffsize = 128; /* buffer size */
- char *result, *tmp;
- unsigned int length = 0; /* length of string read */
- int last;
-
- result = mem(buffsize+1);
-
- loop{
- tmp = result+length;
- if(NULL==fgets(tmp, buffsize+1, fd)){
- /* no more characters to read; EOF reached */
- if(tmp==result){
- /* we never read anything! */
- free(result);
- return(NULL);
- }
- break;
- } else {
- last = strlen(tmp);
- if('\n'==*(tmp+last-1)){
- /* found a newline */
- *(tmp+last-1)='\0';
- break;
- }
- /* still more to read */
- length += buffsize;
- realloc(result, length+buffsize);
- }
- }
- /* trim off excess buffer */
- rmem(result, strlen(result)+1);
- return(result);
- }
-